package com.tianhua.javs.core.func.script;

import com.tianhua.javs.client.bean.JavsSegmentBean;
import com.tianhua.javs.client.bean.ScriptVarMappingBean;
import com.tianhua.javs.client.bean.TranslateResultBean;
import com.tianhua.javs.client.func.IScriptFunc;
import com.tianhua.javs.client.func.ITranslatorFunc;
import com.tianhua.javs.core.func.TranslatorContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Description:
 * date: 2022/2/11
 *
 * @author shenshuai
 * @version 1.0.0
 * @since JDK 1.8
 */
@Component(value = "conditionFunc")
public class ConditionFunc implements IScriptFunc {

    @Autowired
    private TranslatorContainer translatorContainer;

    @Override
    public TranslateResultBean translate(String keyWardStr, Map<String, List<ScriptVarMappingBean>> javsVarMap) {
        //if(null(a) || null(b))
        //ifnull(a)
        //ifnull(!a)
        String tempSegment = keyWardStr;
       /* if (trimSegment.startsWith("if(")) {
            tempSegment = replace(tempSegment, javsVarMap, "", "");
        }*/

        Component component = ConditionFunc.class.getAnnotation(Component.class);
        List<ITranslatorFunc> translatorFuncList = translatorContainer.getByScriptFuncBean(component.value());
        for (ITranslatorFunc translatorFunc : translatorFuncList) {
            JavsSegmentBean javsSegmentBean = translatorFunc.translate(tempSegment);
            if (javsSegmentBean.match()) {
                return TranslateResultBean.getInstance(javsSegmentBean.getSegment());
            }
        }
        return TranslateResultBean.getInstance(tempSegment);
    }

}
